package com.dfbz.netctoss.controller;

import com.dfbz.netctoss.Result;
import com.dfbz.netctoss.service.AdminInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/common")
public class CommonController {

    @Autowired
    AdminInfoService service;

    /**
     * 身份验证
     * @param username
     * @param password
     * @param checkCode
     * @return Result
     */
    @RequestMapping("login")
    @ResponseBody
    public Result login(String username, String password, String checkCode){
        //1.判断验证码是否相同
        if(checkCode.equals("8251")){
            //添加用户认证信息
            Subject subject = SecurityUtils.getSubject(); //主体
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
            //进行验证，这里可以捕获异常，然后返回对应信息
            try {
                subject.login(usernamePasswordToken);
                return new Result(true, "登录成功", null);
            } catch (UnknownAccountException e) {
                System.out.println("用户名错误");
                return new Result(false, "用户名或密码错误", null);
            } catch (IncorrectCredentialsException e){
                System.out.println("密码错误");
                return new Result(false, "用户名或密码错误", null);
            }

        }else{
            return new Result(false,"验证码错误",null);
        }
    }

}
